package com.acumenvn.pent.Objects;

import com.acumenvn.pent.Utils.GameInformation;
import com.acumenvn.pent.Objects.CObjectsList;
import com.acumenvn.pent.Objects.CRect;
import com.acumenvn.pent.Objects.CObject;
import com.acumenvn.pent.Objects.CQuadTree;
import com.acumenvn.pent.Objects.AllSprite;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;

enum State_Prinny {
	Jump_Up_Left,		// 0
	Jump_Down_Left,		// 1
	Jump_Up_Right,		// 2
	Jump_Down_Right,	// 3
	Move_Left,			// 4
	Move_Right,			// 5
	Stand_Left,			// 6
	Stand_Right,		// 7
	Shoot_Left,			// 8
	Shoot_Right,		// 9
	Stop_Left,			// 10
	Stop_Right			// 11
}

enum State_Prinny_2 {
	Stand,
	Jump,
	Shoot
}

public class CPrinny {
	// Variables
	private InputProcessor m_inputProcessor;
	public float x;
	public float y;
	public float x_save;
	public float y_save;
	public float x_before;
	public float y_before;
	public float m_height;
	public float m_width;
	public float m_vX;
	public float m_vY;
	public Boolean isJump;
	public int m_heal;
	public CObjectsList m_listObjects;
	public int m_id;
	// Sprite
	public AllSprite m_allSprites;
	public CRect m_rectSprite;
	public Boolean isRight;
	public int m_spriteIndex_X;
	public int m_spriteIndex_Y;
	public int m_spriteIndexStar_X;
	public int[] m_max_spriteIndex_X; // 20
	public long m_spriteDelay;
	public long m_spriteDelay_max;
	public int m_statePrinny;
	// Cammera
	public OrthographicCamera m_camera;
	/************************************************************************/
	/*                              Shoot                                   */
	/************************************************************************/
	int x_prinnyCut;
	int y_prinnyCut;
	int x_kiem;
	int y_kiem;
	int DelayCut;
	int TiLeRenderX;
	int TiLeRenderY;
	int TiLeChemX;
	int TiLeChemY;
	CRect m_rectSpritechem;
	CRect m_rectSpritekiem;
	
	// Constructor
	public CPrinny(float _x , float _y , float _m_height , float _m_width , AllSprite _m_allSprites , OrthographicCamera  _m_camera) {
		// TODO Auto-generated constructor stub
		m_inputProcessor = Gdx.input.getInputProcessor();
		m_allSprites = _m_allSprites;
		x = _x;
		y = _y;
		x_save = _x;
		y_save = _y;
		x_before = _x;
		y_before = _y;
		m_height = _m_height;
		m_width = _m_width;
		m_vX = 0;
		m_vY = 0;
		isJump = false;
		m_camera = new OrthographicCamera(GameInformation.getInstance().getWindowWidth(), GameInformation.getInstance().getWindowHeight());
		m_listObjects = new CObjectsList();
		m_heal = 1;
		initSprites();
	}


	private void initSprites() {
		// TODO Auto-generated method stub
		m_max_spriteIndex_X = new int[20];
		m_max_spriteIndex_X[State_Prinny.Move_Left.ordinal()] = 6;
		m_max_spriteIndex_X[State_Prinny.Move_Right.ordinal()] = 6;
		m_max_spriteIndex_X[State_Prinny.Stop_Left.ordinal()] = 2;
		m_max_spriteIndex_X[State_Prinny.Stop_Right.ordinal()] = 2;
		m_max_spriteIndex_X[State_Prinny.Stand_Right.ordinal()] = 1;
		m_max_spriteIndex_X[State_Prinny.Stand_Left.ordinal()] = 1;
		m_max_spriteIndex_X[State_Prinny.Jump_Down_Left.ordinal()] = 4;
		m_max_spriteIndex_X[State_Prinny.Jump_Down_Right.ordinal()] = 4;
		m_max_spriteIndex_X[State_Prinny.Jump_Up_Left.ordinal()] = 2;
		m_max_spriteIndex_X[State_Prinny.Jump_Up_Right.ordinal()] = 2;
		m_max_spriteIndex_X[State_Prinny.Shoot_Right.ordinal()] = 7;
		m_max_spriteIndex_X[State_Prinny.Shoot_Left.ordinal()] = 7;
		
		x_prinnyCut = 0;
		x_kiem = 0;
		DelayCut = 0;
		m_spriteIndex_X = 0;
		m_spriteIndex_Y = 0;
		m_spriteDelay = 0;
		isRight = true;
		m_statePrinny = State_Prinny_2.Stand.ordinal();
	}
	
	// Update Sprite
	private void updateSprite() {
		
	}
	
	// Move
	public void move() {
		m_vY += GameInformation.getInstance().getPrinnyAccelerationX();
		Boolean flagFriction = true;
		if(Gdx.input.isKeyPressed(Keys.RIGHT))
		{
			MoveRight();
			flagFriction = false;
			isRight = true;
		}
		if(Gdx.input.isKeyPressed(Keys.LEFT))
		{
			MoveLeft();
			flagFriction = false;
			isRight = false;
		}
		if (flagFriction == true)
		{
			m_vX = m_vX*(float)GameInformation.getInstance().getPrinnyFriction();
		}
	}
	
	// Move Right
	void MoveRight()
	{
		if(m_vX < GameInformation.getInstance().getPrinnyMaxVelocity())
		{
			m_vX += GameInformation.getInstance().getPrinnyAccelerationX();
		}
		else
			m_vX = GameInformation.getInstance().getPrinnyMaxVelocity();
	}
	void MoveLeft()
	{
		if(m_vX > - GameInformation.getInstance().getPrinnyMaxVelocity())
		{
			m_vX -= GameInformation.getInstance().getPrinnyAccelerationX();
		}
		else
			m_vX = - GameInformation.getInstance().getPrinnyMaxVelocity();
	}
	// Jump
	void jump()
	{
		
		float VJump = GameInformation.getInstance().getPrinnyJumpVelocity();
		Boolean collisionLeft = CQuadTree.checkRectCollideWithList(new CRect(x-VJump,y-VJump,m_width,m_height),m_listObjects);
		Boolean collisionRight = CQuadTree.checkRectCollideWithList(new CRect(x+VJump,y-VJump,m_width,m_height),m_listObjects);

		if(collisionLeft == true && collisionRight == false)
		{
			m_vX = (float) (VJump*0.4);
		}
		if(collisionLeft == false && collisionRight == true)
		{
			m_vX = (float) (-VJump*0.4);
		}
		m_vY = -VJump;
		m_statePrinny = State_Prinny_2.Jump.ordinal();
	}
	// Sprite Cut
	public void prinnyCut(CQuadTree quadTree) {
		if(isRight == true) {
			y_prinnyCut = 9;
			y_kiem = 1;
			TiLeRenderX = -25;
			TiLeRenderY = -5;
			TiLeChemX = 18;
			TiLeChemY = -5;
		} else {
			y_prinnyCut = 8;
			y_kiem = 0;
			TiLeRenderX = -40;
			TiLeRenderY = -5;
			TiLeChemX = -25;
			TiLeChemY = -5;
		}
		if(x_prinnyCut < m_max_spriteIndex_X[y_prinnyCut]) {
			DelayCut++;
			m_rectSpritechem = new CRect((float)(x_prinnyCut*56),(float)(y_prinnyCut*56),56,56);	
			m_rectSpritekiem = new CRect((float)(x_kiem*108),(float)(y_kiem*64),108,64);
			if(DelayCut >= 2) {
				x_kiem++;
				x_prinnyCut++;
				DelayCut = 0;
				m_listObjects.resetListObjects();
				quadTree.setObjectsList(m_listObjects, new CRect(x + TiLeRenderX ,y + TiLeRenderY ,108,64));
				int cutting = 0;
				do 
				{
					cutting = 0;
					CObject ob_check = CQuadTree.objectCheckRectWithListCut(new CRect(x + TiLeChemX ,y + TiLeChemY ,60,56),m_listObjects);
					if(ob_check != null)
					{
						if(ob_check.m_style == GameInformation.getInstance().getUnit_Monster1() ||
							ob_check.m_style == GameInformation.getInstance().getUnit_Monster2() || 
							ob_check.m_style == GameInformation.getInstance().getUnit_Monster3()) {
							ob_check.m_health = 0;
							ob_check.m_spriteIndex = 0;
							cutting = 1;
							break;
						}
					}
				} while (cutting == 1);
			}
		} else {
			if(Gdx.input.isKeyPressed(Keys.SPACE)) {
				
			}
		}
		/*
		else
		{
			if (KEYDOWN(keys,DIK_SPACE) && KEYUP(last_keys,DIK_SPACE))
			{
				m_bassSound->Play("Cut",true);
				x_prinnyCut = 0;
				x_kiem = 0;
				DelayCut = 0;
			}	
		}
		 */
	}
	
	public void Death() {
		m_heal = 0;
	}
	
	public void reSpam(CQuadTree quadTree) {
		m_heal = 1;
		x = x_save;
		y = y_save;
		m_vX = 0;
		m_vY = 0;
		quadTree.setHealth();
	}
	
	/************************************************************************/
	/*                                Update                                */
	/************************************************************************/
	public int update(CQuadTree quadTree) {
		float VJump = GameInformation.getInstance().getPrinnyJumpVelocity();
		if(Gdx.input.isKeyPressed(Keys.SPACE)) {
			return 3;
		}
		
		if(m_heal == 0) {
			return 2;
		}
		move();
		prinnyCut(quadTree);
		m_listObjects.resetListObjects();
		quadTree.setObjectsList(m_listObjects, new CRect(x,y,m_width,m_height));
		if(CQuadTree.checkRectCollideWithList(new CRect((x+m_vX),(y+m_vY),m_width,m_height), m_listObjects)) {
			isJump = true;
			if(CQuadTree.checkRectCollideWithList(new CRect(x,y+m_vY,m_width,m_height), m_listObjects) == false) { // Va cham trai phai
				if(Gdx.input.isKeyPressed(Keys.UP)) {
					// play sound
					jump();
				} else {
					m_vX = 0;
					m_vY = (float) (VJump*0.5);
					m_statePrinny = State_Prinny_2.Stand.ordinal();
				}
			} else if(CQuadTree.checkRectCollideWithList(new CRect(x+m_vX,y,m_width,m_height), m_listObjects) == false) { // va cham tren duoi
				if(m_vY > 0) // va cham tren
				{
					if(Gdx.input.isKeyPressed(Keys.UP))
					{
//						m_bassSound->Play("Jump",true);
						m_vY = -VJump;
						m_statePrinny = State_Prinny_2.Jump.ordinal();
					}
					else
					{
						m_vY = 0;
						m_statePrinny = State_Prinny_2.Stand.ordinal();
					}
				}
				else  // va cham duoi
				{
					m_vY = 0;
					isJump = false;
				}
			} else {
				if(Gdx.input.isKeyPressed(Keys.UP))
				{
//					m_bassSound->Play("Jump",true);
					jump();
				}
				else
				{
					m_vX = 0;
					m_vY = 0;
				}
			}
		} else {
			m_statePrinny = State_Prinny_2.Jump.ordinal();
			if(Gdx.input.isKeyPressed(Keys.UP) && isJump) {
//				m_bassSound->Play("Jump",true);
				m_vY = -VJump;
				isJump = false;
			}
		}
		/////////
		float accX = GameInformation.getInstance().getPrinnyAccelerationX();
		float accY = GameInformation.getInstance().getPrinnyAccelerationY();
		if (m_vX < accX && m_vX > - accX)
		{
			m_vX = 0;
		}
		if (m_vY < accY && m_vY > - accY)
		{
			m_vY = 0;
		}
		if(y+m_height+10 > 1100)
		{
			Death();
		}
		
		if(CQuadTree.checkRectCollideWithList(new CRect((x+m_vX),(y+m_vY),m_width,m_height), m_listObjects)) {
			if(CRect.checkStayInsideAnotherRect(new CRect((x+m_vX),(y+m_vY),m_width,m_height), quadTree.m_root.m_rect)) {
				x_before = x;
				y_before = y;
				y += m_vY;
				x += m_vX;
			} else if(!CRect.checkStayInsideAnotherRect(new CRect((x+m_vX),y,m_width,m_height), quadTree.m_root.m_rect)) {
				m_vY = 0;
			}
			y += m_vY;
			x += m_vX;
		} else if(CQuadTree.checkRectCollideWithList(new CRect(x_before,y_before,m_width,m_height),m_listObjects)) {
			Death();
		} else {
			x = x_before;
			y = y_before;
			m_vX = 0;
			m_vY = 0;
		}
		updateSprite();
		
		/////////////////
		CObject ob_check = CQuadTree.objectCheckRectWithListCheckItems(new CRect(x,y+1,m_width,m_height),m_listObjects);
		if(ob_check != null) {
			if(ob_check.m_style == GameInformation.getInstance().getUnit_Monster1() || 
					ob_check.m_style == GameInformation.getInstance().getUnit_Monster2() || 
					ob_check.m_style == GameInformation.getInstance().getUnit_Monster3() || 
					ob_check.m_style == GameInformation.getInstance().getUnit_Fire1() || 
					ob_check.m_style == GameInformation.getInstance().getUnit_Trap()) {
				Death();
			} else if(ob_check.m_style == GameInformation.getInstance().getUnit_Save()) {
				ob_check.m_health = 0;
				x_save = ob_check.m_rect.getLeft();
				y_save = ob_check.m_rect.getTop();
				return 4;
			} else if(ob_check.m_style == GameInformation.getInstance().getUnit_End()) {
				return 1;
			}
		}
		return 0;
	}
	
	public void updateSpriteJump() {
		if(isRight == true) {
			if(m_vY < 0) {
				if(m_spriteIndex_Y != State_Prinny.Jump_Up_Right.ordinal()) {
					m_spriteIndex_Y = State_Prinny.Jump_Up_Right.ordinal();
					m_spriteIndex_X = 0;
					m_spriteIndexStar_X = 0;
				}
			} else if(m_spriteIndex_Y != State_Prinny.Jump_Down_Right.ordinal()) {
				m_spriteIndex_Y = State_Prinny.Jump_Down_Right.ordinal();
				m_spriteIndex_X = 0;
				m_spriteIndexStar_X = 0;
			}
		} else {
			if(m_vY < 0) {
				if(m_spriteIndex_Y != State_Prinny.Jump_Up_Left.ordinal()) {
					m_spriteIndex_Y = State_Prinny.Jump_Up_Left.ordinal();
					m_spriteIndex_X = 0;
					m_spriteIndexStar_X = 0;
				}
			} else {
				if(m_spriteIndex_Y != State_Prinny.Jump_Down_Left.ordinal()) {
					m_spriteIndex_Y = State_Prinny.Jump_Down_Left.ordinal();
					m_spriteIndex_X = 0;
					m_spriteIndexStar_X = 0;
				}
			}
		}
	}
	
	public void updateSpriteStand() {
		if (m_vX == 0)
		{
			if(isRight == true && m_spriteIndex_Y != State_Prinny.Stand_Right.ordinal())
			{
				m_spriteIndex_Y = State_Prinny.Stand_Right.ordinal();
				m_spriteIndex_X = 7;
				m_spriteIndexStar_X = 7;
				m_spriteDelay_max = 8;
			}
			if (isRight == false && m_spriteIndex_Y != State_Prinny.Stand_Left.ordinal())
			{
				m_spriteIndex_Y = State_Prinny.Stand_Left.ordinal();
				m_spriteIndex_X = 7;
				m_spriteIndexStar_X = 7;
				m_spriteDelay_max = 8;
			}
		}
		if(m_vX >0)
		{
			if (isRight == true || (Gdx.input.isKeyPressed(Keys.LEFT)))
			{
				if (m_spriteIndex_Y != State_Prinny.Move_Right.ordinal())
				{
					m_spriteIndex_Y = State_Prinny.Move_Right.ordinal();
					m_spriteIndex_X = 0;
					m_spriteIndexStar_X = 0;
				}
			}
			else
			{
				if (m_spriteIndex_Y != State_Prinny.Stop_Right.ordinal() && Gdx.input.isKeyPressed(Keys.UP))
				{
					m_spriteIndex_Y = State_Prinny.Stop_Right.ordinal();
					m_spriteIndex_X = 0;
					m_spriteIndexStar_X = 0;
					m_spriteDelay_max = 20;
				}
			}
		}
		if(m_vX <0)
		{
			if (isRight == false || (Gdx.input.isKeyPressed(Keys.LEFT)))
			{
				if (m_spriteIndex_Y != State_Prinny.Move_Left.ordinal())
				{
					m_spriteIndex_Y = State_Prinny.Move_Left.ordinal();
					m_spriteIndex_X = 0;
					m_spriteIndexStar_X = 0;
				}
			}
			else
			{
				if (m_spriteIndex_Y != State_Prinny.Stop_Left.ordinal() && m_inputProcessor.keyUp(Keys.LEFT))
				{
				
					m_spriteIndex_Y = State_Prinny.Stop_Left.ordinal() ;
					m_spriteIndex_X = 0;
					m_spriteIndexStar_X = 0;
					m_spriteDelay_max = 20;
				}
			}
		}
	}
}
